<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item prop="vehicleId">
        <el-select v-model="queryParams.vehicleId" placeholder="请选择餐车出货" clearable>
          <el-option v-for="item in vehicle" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="vehicleNo">
        <el-select v-model="queryParams.vehicleNo" placeholder="请选择下架餐车编号" clearable>
          <el-option v-for="item in vehicle" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="vehicleName">
        <el-select v-model="queryParams.vehicleName" placeholder="请选择下架餐车名称" clearable>
          <el-option v-for="item in vehicle" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="boxIndex">
        <el-select v-model="queryParams.boxIndex" placeholder="请选择下架餐车规格" clearable>
          <el-option v-for="item in vehicle" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="下架人" prop="downPerson">
        <el-input
          v-model="queryParams.downPerson"
          placeholder="请输入下架人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="downTime">
        <el-date-picker
          v-model="queryParams.downTime"
          clearable
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下架日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          v-hasPermi="['vehicle:box:goodsDownVehicleBox:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd">创建</el-button>
        <!--<el-button
          v-hasPermi="['vehicle:box:goodsUpVehicleBox:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport">导出</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="goodsDownVehicleBoxList">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="{$index}">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="餐车ID" align="left" header-align="center" prop="vehicleId" />
      <el-table-column label="餐车编号" align="center" prop="vehicleNo" />
      <el-table-column label="餐车名称" align="center" prop="vehicleName" />
      <el-table-column label="柜格索引" align="center" prop="boxIndex" />
      <el-table-column label="柜格上架ID" align="center" prop="upGoodsId" />
        <template slot-scope="scope">
          <dict-tag :options="dict.type.out_vehicle_status" :value="scope.row.states" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="下架人" align="center" prop="downPerson" />
      <el-table-column label="下架时间" align="center" prop="downTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.downTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding" width="300">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['vehicle:box:goodsDownVehicleBox:edit']"
            size="mini"
            type="primary"
            @click="handleView(scope.row)"
          >详情</el-button>
          <template v-if="scope.row.states===0">
            <el-button
              v-hasPermi="['vehicle:box:goodsDownVehicleBox:edit']"
              size="mini"
              type="primary"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              v-hasPermi="['vehicle:box:goodsDownVehicleBox:edit']"
              size="mini"
              type="primary"
              @click="handleVehicleBoxDown(scope.row)"
            >出库</el-button>
            <el-button
              v-hasPermi="['vehicle:box:goodsDownVehicleBox:remove']"
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品下架对话框 -->

    <vehicle-goods-list :id="id" :title="title" :visible.sync="open" type="out" :mode="mode" @ok="onOk" />
  </div>
</template>

<script>
import { listGoodsDownVehicleBox, getGoodsDownVehicleBox, delGoodsDownVehicleBox, addGoodsDownVehicleBox, updateGoodsDownVehicleBox, updateDownVehicleBox } from '@/api/vehicle/goodsDownVehicleBox'
import { listAllVehicle } from '@/api/vehicle/vehicle'
import VehicleGoodsList from '../components/VehicleGoodsList.vue'

export default {
  name: 'GoodsDownVehicleBox',
  dicts: ['out_vehicle_status'],
  components: {
    VehicleGoodsList
  },
  data() {
    return {
      // 按钮loading
      // buttonLoading: false,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品下架表格数据
      goodsDownVehicleBoxList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleId: undefined,
        vehicleNo: undefined,
        vehicleName: undefined,
        boxIndex: undefined,
        upGoodsId: undefined,
        downPerson: undefined,
        downTime: undefined
      },
      vehicle: [],
      mode: null,
      id: '',
      // 表单参数
      form: {}
      // 表单校验
      // rules: {
      //   id: [
      //     { required: true, message: '出库id不能为空', trigger: 'blur' }
      //   ],
      //   vehicleId: [
      //     { required: true, message: '餐车id不能为空', trigger: 'blur' }
      //   ],
      //   outVehicleOrder: [
      //     { required: true, message: '出库单号不能为空', trigger: 'blur' }
      //   ],
      //   states: [
      //     { required: true, message: '0:待出库;1:已出库；不能为空', trigger: 'blur' }
      //   ],
      //   outPerson: [
      //     { required: true, message: '出库人不能为空', trigger: 'blur' }
      //   ],
      //   outTime: [
      //     { required: true, message: '出库时间不能为空', trigger: 'blur' }
      //   ],
      //   remarks: [
      //     { required: true, message: '备注不能为空', trigger: 'blur' }
      //   ],
      //   outDay: [
      //     { required: true, message: '不能为空', trigger: 'blur' }
      //   ]
      // }
    }
  },
  created() {
    this.getList()
    this.listAllVehicle()
  },
  methods: {
    listAllVehicle() {
      listAllVehicle().then(({ data }) => {
        this.vehicle = data
      })
    },
    /** 查询商品下架列表 */
    getList() {
      this.loading = true
      listGoodsDownVehicleBox(this.queryParams).then(response => {
        this.goodsDownVehicleBoxList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        vehicleId: undefined,
        vehicleNo: undefined,
        vehicleName: undefined,
        boxIndex: undefined,
        upGoodsId: undefined,
        downPerson: undefined,
        downTime: undefined,
        remarks: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
      this.id = ''
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.mode = 'add'
      this.reset()
      this.open = true
      this.title = '创建下架单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.mode = 'update'
      this.loading = true
      this.reset()
      this.id = row.id
      this.open = true
      this.title = '编辑下架单'
      getGoodsDownVehicleBox(id).then(response => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改商品下架'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除？').then(() => {
        this.loading = true
        return delGoodsDownVehicleBox(id)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vehicle/goodsDownVehicleBox/export', {
        ...this.queryParams
      }, `goodsDownVehicleBox_${new Date().getTime()}.xlsx`)
    },
    onOk() {
      if (this.mode !== 'view') {
        this.getList()
      }
    },
    handleVehicleBoxDown(row) {
      this.$modal.confirm('确定出库吗？').then(() => {
        this.loading = true
        return updateDownVehicleBox({ ...row })
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('出库成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    handleView(row) {
      this.mode = 'view'
      this.reset()
      this.id = row.id
      this.open = true
      this.title = '出库单详情'
    }
  }
}
</script>
